package com.linkage.jz.bankruptcy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.utils.LogUtil;

/**
 * @author AFAP
 * 可折叠和收起的TextVierw
 */
public class ExpandableTextView extends android.support.v7.widget.AppCompatTextView {
    public ExpandableTextView(Context context) {
        super(context);
    }

    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public int getLineCount() {
        return super.getLineCount();
    }

    @Override
    public TextUtils.TruncateAt getEllipsize() {
        return super.getEllipsize();
    }

    boolean isInit = false;
    int orgMaxLines = 1;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isEllipsis() && !isInit) {
            isInit = true;
            orgMaxLines = getMaxLines();
            setFocusable(true);
            setClickable(true);

            setOnTouchListener(mOnTouchListener);
            Drawable bottomDrawable = getResources().getDrawable(R.drawable.ic_textview_expand);
            bottomDrawable.setBounds(0, 0, bottomDrawable.getMinimumWidth(), bottomDrawable.getMinimumHeight());

            setCompoundDrawables(null, null, null, bottomDrawable);
        }
    }

    // 是否有省略号
    private boolean isEllipsis() {
        Layout layout = getLayout();
        if (layout != null && layout.getEllipsisCount(getLineCount() - 1) > 0) {
            return true;
        }
        return false;
    }

    private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    Drawable drawable = getCompoundDrawables()[3];

                    int[] location = new int[2];
                    getLocationOnScreen(location);

                    int y_start = getHeight() - getPaddingBottom() - drawable.getBounds().height() + location[1];
                    int y_end = getHeight() - getPaddingBottom() + location[1];
                    float yPosition = event.getRawY();
                    if (yPosition >= y_start && yPosition <= y_end) {
                        LogUtil.v("****", " #############################################" + getLineCount());
                        Drawable bottomDrawable;
                        if (isEllipsis()) {
                            bottomDrawable = getResources().getDrawable(R.drawable.ic_textview_colspan);
                            setMaxLines(Integer.MAX_VALUE);
                        } else {
                            bottomDrawable = getResources().getDrawable(R.drawable.ic_textview_expand);
                            setMaxLines(orgMaxLines);
                        }
                        bottomDrawable.setBounds(0, 0, bottomDrawable.getMinimumWidth(),
                                bottomDrawable.getMinimumHeight());

                        setCompoundDrawables(null, null, null, bottomDrawable);
                        return true;
                    }
                    break;
            }
            return false;
        }
    };

}
